﻿using GameStructure.Animation;
using GameStructure.Contracts;
using GameStructure.Core;
using GameStructure.Types;
using Tao.OpenGl;

namespace GameStructure.GaneState
{
    public class TweenTestState : IGameObject
    {
        private readonly Tween _tween = new Tween(0, 256, 5, Tween.EaseInExpo);
        private readonly Tween _alphaTween = new Tween(0, 1, 5, Tween.EaseInCirc);
        private Color _color = new Color(1, 1, 1, 0);
        private readonly Sprite _sprite = new Sprite();
        private readonly Renderer _renderer = new Renderer();

        public TweenTestState(TextureManager textureManager)
        {
            _sprite.Texture = textureManager.Get("face_alpha");
            _sprite.SetHeight(0);
            _sprite.SetWidth(0);
        }

        public void Update(double elapsedTime)
        {
            if (_tween.IsFinished()) return;

            _tween.Update(elapsedTime);
            _sprite.SetWidth((float)_tween.Value());
            _sprite.SetHeight((float)_tween.Value());

            if (_alphaTween.IsFinished()) return;
            
            _alphaTween.Update(elapsedTime);
            _color.Alpha = (float) _alphaTween.Value();
            _sprite.SetColor(_color);
        }

        public void Render()
        {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            _renderer.DrawSprite(_sprite);
            _renderer.Render();
        }
    }
}